package com.david.client.ui;

import com.david.client.ERP;
import com.david.client.rpc.ILoginService;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.core.Ext;
import com.gwtext.client.data.Field;
import com.gwtext.client.data.Record;
import com.gwtext.client.data.RecordDef;
import com.gwtext.client.data.StringField;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.ButtonConfig;
import com.gwtext.client.widgets.LayoutDialog;
import com.gwtext.client.widgets.LayoutDialogConfig;
import com.gwtext.client.widgets.TabPanel;
import com.gwtext.client.widgets.TabPanelItem;
import com.gwtext.client.widgets.Toolbar;
import com.gwtext.client.widgets.ToolbarButton;
import com.gwtext.client.widgets.ToolbarTextItem;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.event.TabPanelItemListenerAdapter;
import com.gwtext.client.widgets.form.FieldSetConfig;
import com.gwtext.client.widgets.form.Form;
import com.gwtext.client.widgets.form.FormConfig;
import com.gwtext.client.widgets.form.TextField;
import com.gwtext.client.widgets.form.TextFieldConfig;
import com.gwtext.client.widgets.form.VType;
import com.gwtext.client.widgets.layout.BorderLayout;
import com.gwtext.client.widgets.layout.ContentPanel;
import com.gwtext.client.widgets.layout.ContentPanelConfig;
import com.gwtext.client.widgets.layout.LayoutRegionConfig;

public class LoginPanel {
	final Label errMsg = new Label();

	public LayoutDialog getViewPanel() {

		LayoutRegionConfig center = new LayoutRegionConfig() {
			{
				setAutoScroll(true);
				setTabPosition("top");
				setCloseOnTab(true);
				setAlwaysShowTabs(true);
			}
		};

		final LayoutDialog dialog = new LayoutDialog(new LayoutDialogConfig() {
			{
				setModal(true);
				setWidth(500);
				setHeight(350);
				setShadow(true);
				setResizable(false);
				setClosable(false);
				setProxyDrag(true);
				setTitle("Sign in");
			}
		}, center);

		final BorderLayout layout = dialog.getLayout();
		layout.beginUpdate();

		ContentPanel signInPanel = new ContentPanel(Ext.generateId(), "Sign In");
		final LoginForm signInForm = new LoginForm();

		VerticalPanel signInWrapper = new VerticalPanel() {
			{
				setSpacing(30);
				setWidth("100%");
				setHorizontalAlignment(VerticalPanel.ALIGN_CENTER);
			}
		};

		signInWrapper.add(signInForm);
		errMsg.setStyleName("errmsg");
		signInWrapper.add(new ContentPanel() {
			{
				add(errMsg);
			}
		});

		signInPanel.add(signInWrapper);
		layout.add(LayoutRegionConfig.CENTER, signInPanel);

		ContentPanel registerPanel = new ContentPanel(Ext.generateId(),
				new ContentPanelConfig() {
					{
						setTitle("Register");
						setBackground(true);
					}
				});

		final Form registerForm = getRegistrationForm();
		VerticalPanel registerWrapper = new VerticalPanel() {
			{
				setSpacing(30);
				setWidth("100%");
				setHorizontalAlignment(VerticalPanel.ALIGN_CENTER);
			}
		};
		registerWrapper.add(registerForm);

		registerPanel.add(registerWrapper);
		layout.add(LayoutRegionConfig.CENTER, registerPanel);

		final Toolbar tb = new Toolbar("my-tb");
		tb.addButton(new ToolbarButton("About", new ButtonConfig()));
		tb.addSeparator();
		tb.addItem(new ToolbarTextItem("Copyright &copy; 2007"));

		ContentPanel infoPanel = new ContentPanel(Ext.generateId(),
				new ContentPanelConfig() {
					{
						setTitle("Info");
						setClosable(true);
						setBackground(true);
						setToolbar(tb);
					}
				});
		infoPanel.setContent("<p>Some content goes here</p>");

		layout.add(LayoutRegionConfig.CENTER, infoPanel);

		layout.endUpdate();

		final Button signInBtn = dialog.addButton("Sign in");
		signInBtn.addButtonListener(new ButtonListenerAdapter() {
			public void onClick(Button button, EventObject e) {
				RecordDef recordDef = new RecordDef(new Field[] {
						new StringField("username"),
						new StringField("password"), });

				Record record = recordDef.createRecord(new Object[] {});
				signInForm.updateRecord(record);
				String userName = record.getAsString("username");
				String passWord = record.getAsString("password");
				// call server service
				ILoginService.Util.getInstance().login(userName, passWord,
						new AsyncCallback() {

							public void onFailure(Throwable arg0) {

							}

							public void onSuccess(Object arg0) {
								Integer retVal = (Integer) arg0;
								if (retVal.intValue() == ILoginService.SUCCESS) {
									dialog.destroy(true);
									ERP.replace(new MainPanel());
								} else {
									switch (retVal.intValue()) {
									case ILoginService.NAMENOTEXISTS: 
										errMsg.setText("Name Not Exist");
										signInForm.focusUserName();
										break;
									case ILoginService.PASSWDINVAID:
										errMsg.setText("Invalid Password");
										signInForm.focusPassword();
										break;
									default:
										errMsg.setText("Login failed!");
										break;
									}
								}

							}

						});
			}
		});

		final Button registerBtn = dialog.addButton("Register");
		registerBtn.addButtonListener(new ButtonListenerAdapter() {
			public void onClick(Button button, EventObject e) {
				registerForm.submit();
			}
		});
		registerBtn.hide();

		dialog.addButton(new Button(new ButtonConfig() {
			{
				setText("Cancel");
				setButtonListener(new ButtonListenerAdapter() {
					public void onClick(Button button, EventObject e) {
						signInForm.reset();
						registerForm.reset();
						dialog.hide();
					}
				});
			}
		}));

		TabPanel tabPanel = layout.getRegion(LayoutRegionConfig.CENTER)
				.getTabs();

		tabPanel.getTab(0).addTabPanelItemListener(
				new TabPanelItemListenerAdapter() {
					public void onActivate(TabPanelItem tab) {
						dialog.setTitle("Sign In");
						registerBtn.hide();
						signInBtn.show();
					}
				});

		tabPanel.getTab(1).addTabPanelItemListener(
				new TabPanelItemListenerAdapter() {
					public void onActivate(TabPanelItem tab) {
						dialog.setTitle("Register");
						signInBtn.hide();
						registerBtn.show();
						tab.getTextEl().highlight();
					}
				});

		tabPanel.getTab(2).addTabPanelItemListener(
				new TabPanelItemListenerAdapter() {
					public void onActivate(TabPanelItem tab) {
						dialog.setTitle("Info");
						registerBtn.hide();
						signInBtn.hide();
					}
				});

		return dialog;
	}

	public static class LoginForm extends Form {
		private TextField userName = new TextField(new TextFieldConfig() {
			{
				setFieldLabel("Username");
				setName("username");
				setWidth(175);
				setAllowBlank(false);
			}
		});

		private TextField passWord = new TextField(new TextFieldConfig() {
			{
				setFieldLabel("Password");
				setName("password");
				setWidth(175);
				setPassword(true);
				setAllowBlank(false);
			}
		});

		public LoginForm() {
			super(new FormConfig() {
				{
					setWidth(300);
					setLabelWidth(75);

				}
			});
			fieldset("Sign In");

			add(userName);

			add(passWord);
			end();
			render();
		}

		public void focusUserName() {
			userName.focus(true);
		}

		public void focusPassword() {
			passWord.focus(true);
		}
	}

	// private Form getSignInForm() {
	// Form form = new Form(new FormConfig() {
	// {
	// setWidth(300);
	// setLabelWidth(75);
	//
	// }
	// });
	//
	// return form;
	// }
	//
	public Form getRegistrationForm() {
		Form form = new Form("form-ct3", new FormConfig() {
			{
				setWidth(400);
				setLabelWidth(75);
				setLabelAlign("right");
			}
		});

		form.fieldset(new FieldSetConfig() {
			{
				setLegend("Register");
			}
		});
		form.add(new TextField(new TextFieldConfig() {
			{
				setFieldLabel("User Name");
				setName("uname");
				setWidth(200);
				setAllowBlank(false);
			}
		}));

		form.add(new TextField(new TextFieldConfig() {
			{
				setFieldLabel("First Name");
				setName("name");
				setWidth(200);
				setAllowBlank(false);
			}
		}));

		form.add(new TextField(new TextFieldConfig() {
			{
				setFieldLabel("Password");
				setName("password");
				setPassword(true);
				setAllowBlank(false);
				setWidth(200);
			}
		}));

		form.add(new TextField(new TextFieldConfig() {
			{
				setFieldLabel("Email");
				setName("email");
				setVtype(VType.EMAIL);
				setWidth(200);
			}
		}));
		form.end();
		form.render();
		return form;
	}
}
